<?php
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/MatrixColorsShapes.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/database.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/includes/define.php");
	
	class DataManager{
		
		var $pairs;
		var $panas;
		
		function DataManager(){
			$matrix = new MatrixColorsShapes();
			$matrix->createMatrix();
			$this->pairs = array();
			foreach(array_keys($matrix->matrix) as $colorPair){
				foreach(array_keys(reset($matrix->matrix)) as $shape){
					$this->pairs[$colorPair][$shape] = "";
				}
			}
			$db = new Database(MYSQL_DB_HOST,MYSQL_DB_LOGIN,MYSQL_DB_PASSWORD,MYSQL_DB_NAME);
			$db->connect();
			$onePanasAnswer = $db->query("SELECT * FROM panasAnswers LIMIT 1;");
			if(mysql_num_rows($onePanasAnswer) > 0){
				$onePanasAnswer = mysql_fetch_assoc($onePanasAnswer);
				unset($onePanasAnswer['panasId']);unset($onePanasAnswer['participantSessionId']);
				$this->panas = array();
				foreach(array_keys($onePanasAnswer) as $value){
					$value = utf8_encode($value);
					$this->panas[$value] = $value;
				}
			}
			else
			{
				$onePanasAnswer = array();
			}
		}
		
		function makeHeaders(){
			$pairsColumn = array(
				"Statut de la session" => array("" => array("" => array("" => ""))),
				"Id de la session" => array("" => array("" => array("" => ""))),
				"Date de la session" => array("" => array("" => array("" => ""))),
				"Induction" => array("" => array("" => array("" => ""))), 
				"Id de l'étudiant" => array("" => array("" => array("" => ""))),  
				"Année de naissance" => array("" => array("" => array("" => ""))), 
				"Sexe" => array("" => array("" => array("" => ""))), 
				"" => array("Réponses de la PANAS" => $this->panas),
				"Réponses" => $this->pairs
			);
			return $pairsColumn;
		}
		
		function makeValues($studyId = ""){
			$db = new Database(MYSQL_DB_HOST,MYSQL_DB_LOGIN,MYSQL_DB_PASSWORD,MYSQL_DB_NAME);
			$db->connect();
			$array = array();
			$allStudies = ($studyId == "")? $db->query("SELECT * FROM studies;"):$db->query("SELECT * FROM studies WHERE studyId = '".$studyId."';");
			while($study = mysql_fetch_assoc($allStudies)){
				$allSessions = $db->query("SELECT * FROM sessions WHERE studyId = '".$study['studyId']."';");
				while($session = mysql_fetch_assoc($allSessions)){
					$allparticipantSessions = $db->query("SELECT * FROM participantsSessions WHERE sessionId = '".$session['sessionId']."' ORDER BY sessionId ASC, participantId ASC, isFinished ASC;");
					$essaiParticipant = "";
					$i = 1;
					while($participantSession = mysql_fetch_assoc($allparticipantSessions)){
						if($essaiParticipant != $participantSession['participantId']){
							$i = 1;
							$essaiParticipant = $participantSession['participantId'];
						}
						else
						{
							$i++;
						}
						$allAnswers = $db->query("SELECT * FROM colorAnswers WHERE participantSessionId = '".$participantSession['participantSessionId']."';");
						$answerData = array();
						while($answer = mysql_fetch_assoc($allAnswers)){
							$answerData[$answer['leftColorName'].'-'.$answer['rightColorName']][$answer['shapeName']] = array('chosenColorName' => $answer['chosenColorName'], 'time' => $answer['time']);
						}
						$array[$participantSession['participantSessionId']]['answerData'] = array($answerData);
						$participantInfos = $db->query("SELECT * FROM participants WHERE participantId = '".$participantSession['participantId']."';");
						$array[$participantSession['participantSessionId']]['participantInfos'] = mysql_fetch_assoc($participantInfos);
						$array[$participantSession['participantSessionId']]['sessionInfos'] = $session;
						$panasInfos = $db->query("SELECT * FROM panasAnswers WHERE participantSessionId = '".$participantSession['participantSessionId']."';");
						$panasInfos = mysql_fetch_assoc($panasInfos);
						unset($panasInfos['panasId']);unset($panasInfos['participantSessionId']);
						$array[$participantSession['participantSessionId']]['panasInfos'] = $panasInfos;
						$array[$participantSession['participantSessionId']]['sessionStatus'] = ($participantSession['isFinished'] == 0)? iconv("UTF-8", "Windows-1252//TRANSLIT", "Essai ".$i." : Erroné"):"Essai ".$i." : Valide";
					}
				}
			}
			return $array;
		}
				
	}
?>